package org.zhou.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2    //开启swagger
public class SwaggerConfig {
//    配置了swagger的docke t的bean实例
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("a");
    }

    @Bean
    public Docket docket(Environment   environment){
//        获取项目的环境
//        设置要现实的swagger的环境
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
//        通过 environment.acceptsProfiles(profiles)判断是否处在自己设定的环境中

        return  new Docket(DocumentationType.SWAGGER_2)
                .groupName("zhou test")
                .apiInfo(apiInfo())
                .select()
//                RequestHandlerSelectors 配置要扫描接口的方式
//                basePackage 指定要扫描的包
//                any 扫描全部
//                none 都不扫描
//                withClassAnnotation  扫描类上的注解
//                withMethodAnnotation 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("org.zhou.swagger.controller"))
//                .paths()  过滤什么路径
//                .paths(PathSelectors.ant("/zhou/**"))
                .build()
                .enable(flag)  // 是否启用swagger  默认是true   false则关闭swagger

                ;  //工厂模式 创造

}
    //    配置swagger信息=apiinfo
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "zhou的swagger API文档",
                "多捞啊",
                "1.0",
                "https://www.baidu.com",
//                作者信息
                new Contact("周", "localhost:8080/lao", "zhouxiju521@gamil.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }



}
